/*
 * MadHelix is a Java Swing-based GUI frontend for SoundHelix.
 * Copyright (C) 2017-2019, 2023 UltrasonicMadness
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ultrasonicmadness.madhelix.models;

import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class SongHistoryHandler extends DefaultHandler {
    private ArrayList<SongEntry> history = new ArrayList<>();
    private boolean inEntryList = false;

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) {
        if (qName.equals("madhelix-history")) {
            inEntryList = true;
        } else if (qName.equals("entry") && inEntryList) {
            String name = attributes.getValue("name");
            String style = attributes.getValue("style");
            int length = Integer.parseInt(attributes.getValue("length"));
            long timestamp = Long.parseLong(attributes.getValue("timestamp"));

            SongEntry entry = new SongEntry(name, style, length, timestamp);
            history.add(entry);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) {
        if (qName.equals("madhelix-history")) {
            inEntryList = false;
        }
    }

    public ArrayList<SongEntry> getHistory() {
        return history;
    }
}
